package richy.com.floatingball;

import android.content.Intent;
import android.os.Build;
import android.provider.Settings;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {
    private Button mStartButton;
    private Button mQuitButton;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        if (Build.VERSION.SDK_INT > 23) {
            if (!Settings.canDrawOverlays(this)) {
                Intent intent = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION);
                intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                startActivityForResult(intent, 1);
                Toast.makeText(this, "请先允许FloatBall出现在顶部", Toast.LENGTH_SHORT).show();
            }
        }
    }

    private void initView() {
        mStartButton = (Button) findViewById(R.id.btn_start);
        mQuitButton = (Button) findViewById(R.id.btn_quit);
        mStartButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                checkAccessibility();
                Intent intent = new Intent(MainActivity.this, FloatingBallService.class);
                Bundle bundle = new Bundle();
                bundle.putInt(FloatingBallService.TYPE, FloatingBallService.TYPE_ADD);
                intent.putExtras(bundle);
                startService(intent);
            }
        });
        mQuitButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MainActivity.this, FloatingBallService.class);
                Bundle bundle = new Bundle();
                bundle.putInt(FloatingBallService.TYPE, FloatingBallService.TYPE_DEL);
                intent.putExtras(bundle);
                startService(intent);
            }
        });
    }

    private void checkAccessibility() {
        if (!AccessibilityUtils.isAccessibilitySettingsOn(this)) {
            //引导到辅助功能设置页面
            startActivity(new Intent(Settings.ACTION_ACCESSIBILITY_SETTINGS));
            Toast.makeText(this, "请先开启FloatingBall辅助功能", Toast.LENGTH_SHORT).show();
        }
    }
}
